package com.necer.base;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewbinding.ViewBinding;

import java.util.ArrayList;
import java.util.List;

public abstract class NBaseAdapter<T, VB extends ViewBinding> extends RecyclerView.Adapter<NBaseAdapter<T, VB>.BaseViewHolder> {

    private ArrayList<T> dataList = new ArrayList<>();
    private OnItemClickListener<T> onItemClickListener;

    public abstract VB createBinding(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent);

    public abstract void bind(@NonNull VB binding, @NonNull T item, int position);

    public class BaseViewHolder extends RecyclerView.ViewHolder {

        private final VB binding;

        public BaseViewHolder(@NonNull VB binding) {
            super(binding.getRoot());
            this.binding = binding;

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = getAdapterPosition();
                    if (position != RecyclerView.NO_POSITION) {
                        T item = dataList.get(position);
                        if (onItemClickListener != null) {
                            onItemClickListener.onItemClick(item, position);
                        }
                    }
                }
            });
        }

        public void bind(@NonNull T item, int position) {
            NBaseAdapter.this.bind(binding, item, position);
        }
    }

    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        VB binding = createBinding(inflater, parent);
        return new BaseViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {
        T item = dataList.get(position);
        holder.bind(item, position);
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    /**
     * 设置新的数据
     */
    public void setData(List<T> data) {
        if (data != null) {
            this.dataList = new ArrayList<>(data);
        } else {
            this.dataList.clear();
        }
        notifyDataSetChanged();
    }

    /**
     * 添加单条数据
     */
    public void addItem(T item) {
        addItem(dataList.size(), item);
    }

    public void addItem(@IntRange(from = 0) int position, T item) {
        int finalPosition = Math.min(position, dataList.size());
        dataList.add(finalPosition, item);
        notifyItemInserted(finalPosition);
    }

    public interface OnItemClickListener<T> {
        void onItemClick(T item, int position);
    }

    public void setOnItemClickListener(OnItemClickListener<T> listener) {
        this.onItemClickListener = listener;
    }
}